package com.nd.mr.wordcount.homework;


import org.apache.hadoop.io.Writable;
import org.apache.hadoop.io.WritableComparable;
import org.apache.hadoop.mapreduce.lib.db.DBWritable;

import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Objects;

public class statistics implements DBWritable, WritableComparable<statistics> {

    private static int WorldId = 0;
    private int Id;
    private String date;
    private String region;
    private String location;
    private String increase;
    private String cured;
    private String death;
    @Override
    public void write(DataOutput out) throws IOException {
        out.writeInt(Id);
        out.writeUTF(date);
        out.writeUTF(region);
        out.writeUTF(location);
        out.writeUTF(increase);
        out.writeUTF(cured);
        out.writeUTF(death);
    }

    @Override
    public void readFields(DataInput in) throws IOException {
        Id = in.readInt();
        date = in.readUTF();
        region = in.readUTF();
        location = in.readUTF();
        increase = in.readUTF();
        cured = in.readUTF();
        death = in.readUTF();
    }

    @Override
    public void write(PreparedStatement statement) throws SQLException {
        statement.setInt(1,Id);
        statement.setString(2,date);
        statement.setString(3,region);
        statement.setString(4,location);
        statement.setString(5,increase);
        statement.setString(6,cured);
        statement.setString(7,death);
    }

    @Override
    public void readFields(ResultSet resultSet) throws SQLException {
        int temp = resultSet.getInt("id");
        date = resultSet.getString("date");
        region = resultSet.getString("region");
        location = resultSet.getString("location");
        increase = resultSet.getString("increase");
        cured = resultSet.getString("cured");
        death = resultSet.getString("death");
    }

    public statistics(String date, String region, String location, String increase, String cured, String death){
        this.date = date;
        this.region = region;
        this.location = location;
        this.increase = increase;
        this.cured = cured;
        this.death = death;
    }

    public statistics(){}

    @Override
    public String toString() {
        return Id + "," + date + "," + location + "," + region + "," + increase + "," + cured + "," + death + ",";
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        statistics that = (statistics) o;
        return Objects.equals(date, that.date) &&
                Objects.equals(region, that.region) &&
                Objects.equals(location, that.location) &&
                Objects.equals(increase, that.increase) &&
                Objects.equals(cured, that.cured) &&
                Objects.equals(death, that.death);
    }

    @Override
    public int hashCode() {
        return Objects.hash(date, region, location, increase, cured, death);
    }

    public String getDate() {
        return date;
    }

    public void setDate(String date) {
        this.date = date;
    }

    public String getRegion() {
        return region;
    }

    public void setRegion(String region) {
        this.region = region;
    }

    public String getLocation() {
        return location;
    }

    public void setLocation(String location) {
        this.location = location;
    }

    public String getIncrease() {
        return increase;
    }

    public void setIncrease(String increase) {
        this.increase = increase;
    }

    public String getCured() {
        return cured;
    }

    public void setCured(String cured) {
        this.cured = cured;
    }

    public String getDeath() {
        return death;
    }

    public void setDeath(String death) {
        this.death = death;
    }

    public int getId() {
        return Id;
    }

    public void setId() {
        WorldId++;
        this.Id = WorldId;
    }

    @Override
    public int compareTo(statistics o) {
        /*
        int i = o.date.compareTo(date);

        int j = o.location.compareTo(location);
        int m = o.region.compareTo(region);
        int n = o.increase.compareTo(increase);
        int g = o.cured.compareTo(cured);
        int h = o.death.compareTo(death);
        if (i == 0) {
            if (j == 0) {
                if (m == 0) {
                    if (n == 0) {
                        if (g == 0) {
                            return -h;
                        } else {
                            return -g;
                        }
                    } else {
                        return -n;
                    }
                } else {
                    return -m;
                }
            } else {
                return -j;
            }
        } else {
            return -i;
        }*/
        if(o.Id>this.Id)
            return -1;
        return 1;

    }
}
